package com.test.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.test.bean.Article;
import com.test.util.Page;
import com.test.util.SQLiteUtils;

public class ArticleDAO {
	/**
	 * 通过ＩＤ查询
	 * @param id
	 */
	public static Article selectById(Integer id) {
		Connection conn = SQLiteUtils.getConnection();
		Article article = new Article();
		try {
			Statement stat = conn.createStatement();
			conn.setAutoCommit(false);
			ResultSet rs = stat.executeQuery("select * from t_article where id=" + id);
			
			if (rs != null && rs.next()) {
				article.setId(rs.getInt("id"));
				article.setTitle(rs.getString("title"));
				article.setCategoryId(rs.getInt("category_id"));
				article.setContent(rs.getString("content"));
				article.setCreateTime(rs.getTimestamp("create_time"));
				article.setModTime(rs.getTimestamp("mod_time"));
			}
			conn.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		return article;
	}
	
	/**
	 * insert
	 * @param article
	 */
	public static void insert(Article article) {
		Connection conn = SQLiteUtils.getConnection();
		try {
			PreparedStatement stat = conn.prepareStatement("insert into t_article(title, content, category_id,create_time, mod_time) values(?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS);
			conn.setAutoCommit(false);
			
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			stat.setString(1, article.getTitle());
			stat.setString(2, article.getContent());
			stat.setInt(3, article.getCategoryId());
			stat.setTimestamp(4, timestamp);
			stat.setTimestamp(5, timestamp);
			
			stat.executeUpdate();
			ResultSet rs = stat.getGeneratedKeys();
			if (rs != null && rs.next()) {
				article.setId(rs.getInt(1));
			}
			
			conn.commit();
			conn.close();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		}
	}
	
	
	/**
	 * update
	 * @param article
	 */
	public static void update(Article article) {
		Connection conn = SQLiteUtils.getConnection();
		try {
			PreparedStatement stat = conn.prepareStatement("update t_article set title=?, content=?, category_id=?,mod_time=? where id=?");
			conn.setAutoCommit(false);
			
			Timestamp timestamp = new Timestamp(System.currentTimeMillis());
			stat.setString(1, article.getTitle());
			stat.setString(2, article.getContent());
			stat.setInt(3, article.getCategoryId());
			stat.setTimestamp(4, timestamp);
			stat.setInt(5, article.getId());
			
			stat.executeUpdate();
			
			conn.commit();
			conn.close();
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 取分页
	 * @return
	 */
	public static Page<Article> selectList(int pageNO, int pageSize) {
		Page<Article> page = new Page<Article>();
		List<Article> list = new ArrayList<Article>();
		
		Connection conn = SQLiteUtils.getConnection();
		ResultSet rs = null;
		try {
			PreparedStatement stat = conn.prepareStatement("select * from t_article order by order_num limit ?,?");
			stat.setInt(1, (pageNO-1)*pageSize);
			stat.setInt(2, pageSize);
			
			rs = stat.executeQuery();
			
			if (rs != null) {
				while (rs.next()) {
					Article article = new Article();
					article.setId(rs.getInt("id"));
					article.setTitle(rs.getString("title"));
					article.setCategoryId(rs.getInt("category_id"));
					article.setContent(rs.getString("content"));
					article.setCreateTime(rs.getTimestamp("create_time"));
					article.setModTime(rs.getTimestamp("mod_time"));
					
					list.add(article);
				}
			}
			
			conn.close();
		} catch (Exception e) {
		}
		return page;
	} 
	
	
	/**
	 * delete
	 * @param articleId
	 */
	public static int delete(Integer id) {
		Connection conn = SQLiteUtils.getConnection();
		try {
			PreparedStatement stat = conn.prepareStatement("delete from t_article  where id=?");
			
			conn.setAutoCommit(false);
			
			stat.setInt(1, id);
			
			int row = stat.executeUpdate();
			
			conn.commit();
			conn.close();
			return row;
		} catch (Exception e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		}
	}
}
